import E from 'wangeditor'
import $axios from "@/plugins/axios"
let menus = [
  'head',  // 标题
  'bold',  // 粗体
  'fontSize',  // 字号
  'fontName',  // 字体
  'italic',  // 斜体
  'underline',  // 下划线
  'strikeThrough',  // 删除线
  'foreColor',  // 文字颜色
  'backColor',  // 背景颜色
  'link',  // 插入链接
  'justify',  // 对齐方式
  'image',  // 插入图片
]
class Editor{
  constructor (){
    this.editor = null
    this.menus = menus
  }
  init(domId){
    this.editor = new E(domId);
    this.editor.customConfig.menus = this.menus
    this.editor.customConfig.uploadImgMaxLength = 1
    // 设置图片上传
    this._customUploadImg()
    this.editor.create();
    return this.editor
  }
  _customUploadImg(){
    this.editor.customConfig.customUploadImg = (files, insert) => {
      const formData = new FormData();
      formData.append('file', files[0]);
      $axios('/web/v1/resource/file','POST', formData).then(res => {
        const result = res.data;
        if (!isNaN(Number(result))) {
          insert(`/web/v1/resource/file?fileId=${result}`)
        }
      })
    }
  }
  setHtml(html){
    this.editor.txt.html(html)
  }
  clearHtml(){
    this.editor.txt.clear()
  }
  getHtml(){
    return this.editor.txt.html()
  }
  getText(){
    return this.editor.txt.text()
  }

}
export default Editor